package com.gome.han.bigdata.spark.core.rdd.operation.transformation

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @author Hanpeng
 * @date 2021/1/12 22:08
 * @description:
 *              mapPartitionsWithIndex 主要是增加了分区号
 */
object MapPartitionsWithIndexOperation {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)

    // TODO 算子 - mapPartitions
    val rdd = sc.makeRDD(List(1,2,3,4), 2)
    // 【1，2】，【3，4】  index表示索引编号
    val mpiRDD = rdd.mapPartitionsWithIndex(
      (index, iter) => {
        if ( index == 1 ) {
          iter
        } else {
          //Nil 是一个空的List
          Nil.iterator
        }
      }
    )
    mpiRDD.collect().foreach(println)


    sc.stop()
  }
}
